_package("caf.ui");

_import("caf.ui.TplPane");

/**
 * Page基础类
 */
_class("Page", TplPane, function(){
	this._init = function(){
		_super._init.call(this);
		//this._data = null;  //配置信息
		this._app = __context__.application;
		this._parentPage = null;  //父Page组件
		this._params = null;  //参数
		this._pid = "";       //pageid
		this._url = "";       //请求服务的url地址
	};
	this.create = function(parent){
		this.setParent(parent);
		var obj = this.createTplElement(parent, this.getTpl());
		this.bind(obj/*, this._app*/);
		this.setVisible(false);
		//[TODO]应该让Page组件自行方便的去调用这个方法
		//[TODO]这两句执行太靠后了，很容易出问题
		this.initComponents();  //初始化内部组件
		this.initActionElements(/*this._self, this*/);  //初始化动作元素
		return obj;
	};
	/**
	 * @param {Object} params 传递给Page组件的参数
	 */
	this.reset = function(params){
		if(this._params !== params){
			this.setParams(params);
		}
	};
	this.dispose = function(){
		this._params = null;
		this._data = null;
		this._parentPage = null;
		this._app = null;
		var obj = this._self;
		_super.dispose.apply(this);
		obj.parentNode.removeChild(obj);
		obj = null;
	};
	/*
	this.create = function(parent, html){
		this._self = this.createDomElement(parent, html, ".main");
		if(this._self.hasChildNodes()){
			this.renderTag(this._self);
		}
	};
	this.pageBack = function(){
		window.alert("back");
	};
	*/
	this.setParentPage = function(v){
		this._parentPage = v;
	};
	this.getPid = function(){
		return this._pid;
	};
	this.setPid = function(v){
		this._pid = v;
	};
	this.setData = function(v){
		this._data = v;
		//_super.setData.apply(this, arguments);
		this._pid = v.pid;
		this.setTpl(v.tpl);
	};
	this.setParams = function(v){
		this._params = v;
	};
	/*
	this.onActivate = function(ev){
	};
	this.onDeactivate = function(ev){
	};
	*/
	this.onShow = function(ev){
		this.setZIndex(runtime.getNextZIndex());
		this._self.style.top = "0px";
		this.show();
		if(ev.cbid){
			runtime._task.ready(ev.cbid);
		}
	};
	this.onHide = function(ev){
		//this._self.style.opacity = "0";
		//this._self.style.display = "none";
		this.hide();
		if(ev.cbid){
			runtime._task.ready(ev.cbid);
		}
	};
	this.onAnimateShow = function(ev){
		this.setZIndex(runtime.getNextZIndex());
		this._self.style.top = "0px";
		this.fadeIn(this._self, 300, this, function(){
			this._self.style.opacity = "";
			this.show();
			if(ev.cbid){
				runtime._task.ready(ev.cbid);
			}
		});
	};
	this.onAnimateHide = function(ev){
		this.fadeOut(this._self, 300, this, function(){
			this._self.style.opacity = "";
			this.hide();
			if(ev.cbid){
				runtime._task.ready(ev.cbid);
			}
		});
	};
	/**
	 * 显示Page组件
	 */
	this.show = function(){
		if(this._params && this._params.navmode == "modal"){
			this.showModal(true);
		}else{
			this.setVisible(true);
		}
	};
	/**
	 * 隐藏Page组件
	 */
	this.hide = function(){
		if(this._params && this._params.navmode == "modal"){
			this.showModal(false);
		}else{
			this.setVisible(false);
		}
	};
	/**
	 * 隐藏或显示模态
	 * @param {Boolean} v 模态特性的目标状态(true=显示,false=隐藏)
	 */
	this.showModal = function(v){
		//var panel = this._ownerApp._contentPane.getModalPanel();
		//var panel = runtime._workspace.getModalPanel();
		var panel = this._app.getContentPane().getModalPanel();
		if(v){
			panel.pushTarget(this);
			this.setZIndex(runtime.getNextZIndex());
			//this._self.onclick = function(){this._ptr.showModal(false);};
			panel.setVisible(v);
		}else{
			panel.popTarget();
			//if(!panel.getActiveTarget()){
			//	panel.setVisible(v);
			//}
		}
		this.setVisible(v);
		//this.moveToCenter();
		if(v){
			try{
				this._self.focus();  //设定焦点
			}catch(ex){
			}
			//this.reset(this._req.params);
		}
		panel = null;
	};
	/**
	 * 通用的页面返回动作
	 * @param {String} act 当前动作的名字
	 * @param {HTMLElement} sender 动作的发送者
	 */
	this.do_page_back = function(act, sender){
		this._app._deckPage.pageBack();
	};
});